<template>
  <div class="login-container">
    <div class="login-content">
      <div class="login-desc">
        <div class="desc-top">
          <img src="../../assets/images/logo.png" alt class="icon svg-logo" />
        </div>
        <div class="desc-list">
          <div class="desc-item">专业</div>
          <div class="desc-item">专注</div>
          <div class="desc-item">诚信</div>
          <div class="desc-item">服务</div>
        </div>
      </div>
      <el-form
        class="login"
        autocomplete="off"
        label-position="left"
        ref="loginform"
        :model="form"
        :rules="rules"
      >
        <div class="login-top">管理系统</div>
        <div class="login-center clearfix">
          <el-form-item class="login-center-input" prop="username">
            <el-input
              v-model="form.username"
              type="text"
              :placeholder="usernamefocus ? '' : '请输入您的用户名'"
              @focus="usernamefocus = true"
              @blur="usernamefocus = false"
            />
            <div
              :class="[
                'login-center-input-text',
                usernamefocus ? 'active' : '',
              ]"
            >
              用户名
            </div>
          </el-form-item>
        </div>
        <div class="login-center clearfix">
          <el-form-item class="login-center-input" prop="password">
            <el-input
              v-model="form.password"
              type="password"
              show-password
              @keyup.enter.native="loginIn('loginform')"
              :placeholder="passwordfocus ? '' : '请输入您的密码'"
              @focus="passwordfocus = true"
              @blur="passwordfocus = false"
            />
            <div
              :class="[
                'login-center-input-text',
                passwordfocus ? 'active' : '',
              ]"
            >
              密码
            </div>
          </el-form-item>
        </div>
        <el-button
          type="primary"
          class="login-button"
          @click.native.prevent="loginIn('loginform')"
          :loading="loading"
          >登录</el-button
        >
      </el-form>
    </div>
  </div>
</template>

<script>
import { mapActions } from 'vuex'

export default {
  data() {
    const validateUsername = (rule, value, callback) => {
      if (!value) {
        callback(new Error(' ')) // 用户名长度最少一位
      } else {
        callback()
      }
    }
    const validatePassword = (rule, value, callback) => {
      if (value.length < 1) {
        callback(new Error(' ')) // 密码长度最少一位
      } else {
        callback()
      }
    }
    return {
      loading: false,
      form: {
        username: 'admin',
        password: '123456',
      },
      usernamefocus: false,
      passwordfocus: false,
      rules: {
        username: [
          { required: true, trigger: 'blur', validator: validateUsername },
        ],
        password: [
          { required: true, trigger: 'blur', validator: validatePassword },
        ],
      },
    }
  },
  created() {
    if (this.$route.query.username) {
      this.loading = true

      setTimeout(() => {
        this.$store
          .dispatch('pmsTokenLogin', this.$route.query.username)
          .then(() => {
            this.$router.push({ path: this.redirect || '/' })
            this.loading = false
          }, 1000)
      }).catch(() => {
        this.loading = false
      })
    }
  },
  watch: {
    $route: {
      handler: function(route) {
        this.redirect = route.query && route.query.redirect
      },
      immediate: true,
    },
  },
  methods: {
    ...mapActions({ add_Routes: 'add_Routes' }),
    /*登录*/
    loginIn(formName) {
      this.$refs[formName].validate(async (valid) => {
        if (valid) {
          this.loading = true
          const params = {
            username: this.form.username.trim(),
            password: this.form.password.trim(),
          }
          this.$store
            .dispatch('pmsLogin', params)
            .then(() => {
              this.$router.push({ path: this.redirect || '/' })
              this.loading = false
            })
            .catch(() => {
              this.loading = false
            })
        } else {
          this.$refs[formName].validate()
          this.$router.push({ path: this.redirect || '/' })
          return false
        }
      })
    },
  },
}
</script>

<style lang="scss" scoped>
$logintinge: #101010; // 登陆色调
.login-container {
  position: relative;
  width: 100%;
  height: 100%;
  background: url(../../assets/images/login/bg.jpg) center center;
}
.login-content {
  position: absolute;
  top: 50%;
  left: 50%;
  width: 660px;
  height: 390px;
  transform: translate(-50%, -50%);
  background: #fff;
  box-shadow: 0px 0px 55px 0px rgba(14, 5, 9, 0.48);
  //background: url(../../assets/images/login/box.png) top center;
  .login-desc {
    position: absolute;
    top: 0;
    left: 0;
    width: 260px;
    height: 100%;
    padding-bottom: 50px;
    border-right: 1px solid #dedede;
  }
  .login {
    width: 100%;
    height: 100%;
    padding-left: 260px;
    box-sizing: border-box;
  }
}
// 左描述
.login-desc {
  box-sizing: border-box;
  color: $logintinge;
  display: flex;
  flex-direction: column;
  justify-content: space-between;
  .desc-top {
    padding: 50px 0;
    cursor: pointer;
    display: flex;
    justify-content: center;
    align-items: center;
    //margin-bottom: 40px;
    .svg-logo {
      .selected {
        fill: $logintinge;
      }
    }
    div {
      flex: 1;
      margin-left: 10px;
      h3 {
        font-size: 22px;
        margin-bottom: 5px;
      }
      p {
        font-size: 12px;
      }
    }
  }
  .desc-list {
    flex: 1;
    display: flex;
    flex-direction: column;
    align-items: center;
    justify-content: space-around;
    .desc-item {
      width: 110px;
      height: 30px;
      line-height: 30px;
      text-align: center;
      border-radius: 3px;
      cursor: pointer;
      border: 1px solid #2e2f35;
    }
  }
}
// 右登陆
.login /deep/ {
  .login-top {
    font-size: 24px;
    color: #333333;
    box-sizing: border-box;
    margin: 50px auto;
    text-align: center;
  }
  .login-center {
    width: 320px;
    box-sizing: border-box;
    margin: 30px auto 25px;
    .login-center-img {
      display: block;
      font-size: 25px;
      float: left;
      margin-top: 6px;
      color: #666;
    }
    .login-center-input {
      width: 100%;
      height: 40px;
      position: relative;
      .el-form-item__content {
        height: 40px;
        line-height: 40px;
        .el-input__inner {
          border-radius: 0;
        }
      }
      input {
        z-index: 2;
        transition: all 0.5s;
        padding-left: 10px;
        color: #333333;
        width: 100%;
        height: 40px;
        border: 1px solid #fff;
        border-bottom: 1px solid #cccccc;
        box-sizing: border-box;
        outline: none;
        position: relative;
        &::placeholder {
          color: #757575;
        }
        &:focus {
          border: 1px solid $logintinge;
        }
      }
      .el-input__suffix {
        z-index: 999;
      } // 眼睛
    }
    .login-center-input-text {
      &.active {
        top: 4px;
        z-index: 3;
        opacity: 1;
        margin-top: -15px;
      }
      z-index: 0;
      background: rgba(255, 255, 255, 1);
      position: absolute;
      opacity: 0;
      height: 20px;
      top: 50%;
      margin-top: -10px;
      font-size: 14px;
      left: 5px;
      color: $logintinge;
      line-height: 20px;
      transition: all 0.5s;
    }
  }
  .login-button {
    display: block;
    width: 320px;
    height: 45px;
    margin: 50px auto 40px;
    text-align: center;
    border-radius: 5px;
    border: none;
    color: white;
    cursor: pointer;
    background-color: $logintinge;
  }
}
</style>
